package org.openplaques.android;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class OpenPlaqueDriod extends MapActivity {
	private MapView mapView;
	List<Overlay> mapOverlays;
	Drawable drawable;
	PlaqueOverlay plaqueOverlay;
	MyLocationOverlay me;
	MapController mapController;
	ArrayList<Plaque> plaques;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		mapView = (MapView) findViewById(R.id.mapview);
		LinearLayout layout = (LinearLayout) findViewById(R.id.zoomCtrls);
		layout.addView(mapView.getZoomControls());

		mapController = mapView.getController();
		
		plaques = new ArrayList<Plaque>();
		populatePlaques();

		mapOverlays = mapView.getOverlays();
		// TODO use smaller Icon, or even thumbnail
		drawable = this.getResources().getDrawable(R.drawable.placemark);
		Iterator plaqueIt = plaques.iterator();
		while(plaqueIt.hasNext()) {
			Plaque plaque = (Plaque) plaqueIt.next();
			if (plaque.getPoint() != null) {
				plaqueOverlay = new PlaqueOverlay(drawable);
				//GeoPoint point = new GeoPoint((int)(51.1947386 * 1000000), (int)(1.150118 * 1000000));
				OverlayItem overlayitem = new OverlayItem(plaque.getPoint(), "", "");
				plaqueOverlay.addOverlay(overlayitem);
				mapOverlays.add(plaqueOverlay);
			}
		}
		
		me=new MyLocationOverlay(this, mapView); 
		mapOverlays.add(me);

	}

	@Override 
	public void onResume() { 
	  super.onResume(); 
	  me.enableMyLocation(); 
	  me.runOnFirstFix(new Runnable() {
          public void run() {
              mapController.animateTo(me.getMyLocation());
          }
      });
	}    
	
	@Override 
	public void onPause() { 
	  super.onPause(); 
	  me.disableMyLocation(); 
	} 

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private void populatePlaques() {
		// TODO 
		try { 
		      XmlPullParser xpp=getResources().getXml(R.xml.plaques); 
		      
		      while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) { 
		        if (xpp.getEventType()==XmlPullParser.START_TAG) { 
		          if (xpp.getName().equals("Placemark")) {
		        	  Plaque plaque = new Plaque();
	        		  xpp.next();
		        	  while (xpp.getEventType()!=XmlPullParser.START_TAG) {
		        		  xpp.next();
		        	  }
			          if (xpp.getName().equals("name")) {
			        	  plaque.setName(xpp.nextText());
		        	  }
	        		  xpp.next();
		        	  while (xpp.getEventType()!=XmlPullParser.START_TAG) {
		        		  xpp.next();
		        	  }
		        	  if (xpp.getName().equals("description")) {
			        	  plaque.setDescription(xpp.nextText());
		        	  } 
	        		  xpp.next();
		        	  while (xpp.getEventType()!=XmlPullParser.START_TAG) {
		        		  xpp.next();
		        	  }
		        	  if (xpp.getName().equals("Point")) {
		        		  xpp.next();
			        	  if (xpp.getName().equals("coordinates")) {
				        	  String coordinates = xpp.nextText();
				        	  Log.d("wtf coords", coordinates);
				        	  String[] coordArray = null;
				        	  coordArray = coordinates.split("\\,");
				        	  String latString = (String)coordArray[1];
				        	  String lonString = (String)coordArray[0];
				        	  if (!("").equals(lonString) || !("").equals(latString)) {
					        	  float lat = new Float(latString).floatValue()  * 1000000;
					        	  float lon = new Float(lonString).floatValue()  * 1000000;
					        	  GeoPoint point = new GeoPoint((int)lat, (int)lon);
					        	  plaque.setPoint(point);
				        	  }

			        	  }
		        	  } 

		        	  plaques.add(plaque);
			        	
		          } 
		        } 
		        
		        xpp.next(); 
		      } 
		    } 
		    catch (Throwable t) { 
		      Toast 
		        .makeText(this, "Request failed: "+t.toString(), 4000) 
		        .show(); 
		    } 
	}
}